﻿using WoodAdmin.Common.Extention;
using WoodAdmin.Common.Helper;
using WoodAdmin.Models;
using WoodAdmin.Models.ViewModels;
using WoodAdmin.Repository;

namespace WoodAdmin.Services
{
    public class LoginService : BaseService<SysUser>
    {
       
        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns></returns>
        public byte[] GetVcode()
        {
            string strCode = ValidateCodeHelper.CreateValidateCode(4);
            CacheHelper.CacheInsert(APPKeys.Vercode,strCode);
            byte[] imgBytes = ValidateCodeHelper.CreateValidateGraphic(strCode);
            return imgBytes;
        }

        /// <summary>
        /// 根据用户名和密码获取用户
        /// </summary>
        /// <returns></returns>
        public SysUser GetUserByUserNameAndPass(LoginViewModel model)
        {
            var pass = model.Password.ToMD5String();
            return FindEntity(p => p.Name == model.UserName && p.Password == pass);
        }

        /// <summary>
        /// 根据用户名和密码获取用户查询用户是否存在
        /// </summary>
        /// <returns></returns>
        public bool IsUserExist(LoginViewModel model)
        {
            var pass = model.Password.ToMD5String();
            return FindEntity(p => p.Name == model.UserName && p.Password == pass) != null;
        }
    }
}
